package com.fingard.dsp.bank.directbank.ceb01;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IBalanceCheck;
import com.fingard.dsp.bank.reqrespobj.balancecheck.ReqBalCheckQry;
import com.fingard.dsp.bank.reqrespobj.balancecheck.RetAtsBalCheckQry;
import com.fingard.dsp.bank.reqrespobj.balancecheck.detail.RetAtsBalCheckDetail;
import com.fingard.text.StringHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.util.Calendar;
import java.util.Date;

/**
 * @author Parker on 2019-11-11.
 * @version 1.0
 */
public class CEB01BalanceCheck extends CEB01Base implements IBalanceCheck {
    @Override
    public RetAtsBalCheckQry d_7508_BalCheck(ReqBalCheckQry p_req7508) throws Exception {
//余额对账查询返回ATS的对象
        RetAtsBalCheckQry retAts = new RetAtsBalCheckQry(p_req7508.reqHeader, p_req7508.actNum);
        retAts.writeStart();//写返回ATS的统一头部信息
        try {
            String sPageSize = getBankConfig().getValueAsString("pageSize","");
            int totalNum = 0;
            int startNum = 1;
            int pageSize = Integer.parseInt(StringHelper.hasAnyChar(sPageSize) ? sPageSize : "99");
            do {
                CEB01Header bankHeader = new CEB01Header();
                bankHeader.TransCode = "b2e004010";
                bankHeader.batchID = p_req7508.reqHeader.transSeqID;
                bankHeader.JnlDateTime = new Date();
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                //写入通用的头部节点
                writeCommonHeader_2(xmlWriter, bankHeader);
                xmlWriter.writeStartElement("TransContent");
                XElement tmpXElement = new XElement("ReqData", "");
                tmpXElement.addChild(new XElement("ClientPatchID", p_req7508.reqHeader.transSeqID));
                tmpXElement.addChild(new XElement("AcNo", p_req7508.reqHeader.ownAct));

                String beginDate = Format.Date6YearMonth.format(p_req7508.getYearMonth()) + "01";
                tmpXElement.addChild(new XElement("BeginDate", beginDate));
                Calendar tmpClndrStepTo = Calendar.getInstance();
                tmpClndrStepTo.setTime(Format.Date8Format.parse(beginDate));
                tmpClndrStepTo.set(Calendar.DAY_OF_MONTH, tmpClndrStepTo.getActualMaximum(Calendar.DAY_OF_MONTH));
                String endDate =Format.Date8Format.format(tmpClndrStepTo.getTime());
                tmpXElement.addChild(new XElement("EndDate", endDate));

                tmpXElement.addChild(new XElement("CheckFlag", "0"));
                tmpXElement.addChild(new XElement("StartNum", ""+startNum));
                tmpXElement.addChild(new XElement("RequestNum", ""+pageSize));
                tmpXElement.addChild(new XElement("RedeemType", ""));
                xmlWriter.writeXElement(tmpXElement);
                xmlWriter.writeEndDocument();//写入报文完成

                String[] tmpStrRet = sendToBank_2(xmlWriter.toXmlString(), bankHeader.TransCode);//发送银行

                if(tmpStrRet[0].length() == 0){
                    StringBuilder tmpSbMsg = new StringBuilder();
                    //解析银行返回
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if(tmpBkReader != null){
                        String tmpReturnCode = tmpBkReader.getFirstText("/Transaction/TransContent/ReturnCode");
                        String tmpReturnMsg = tmpBkReader.getFirstText("/Transaction/TransContent/ReturnMsg");
                        String retTotalNum = tmpBkReader.getFirstText("/Transaction/TransContent/TotalNum");
                        totalNum = StringHelper.hasAnyChar(retTotalNum)? Integer.parseInt(retTotalNum) : 0;
                        if (tmpReturnCode != null && "0000".equals(tmpReturnCode)) {
                            startNum += pageSize;
                            while(tmpBkReader.read("/Transaction/TransContent/BatchRespList/RespData")){
                                RetAtsBalCheckDetail tmpRetDetail = new RetAtsBalCheckDetail();
                                tmpRetDetail.actNum = tmpBkReader.readedXElement.getSingleText("AcNo");
                                tmpRetDetail.statementNumber = tmpBkReader.readedXElement.getSingleText("ServiceNum"); //对账单编号
                                tmpRetDetail.yearMonth = tmpBkReader.readedXElement.getSingleText("Year")+tmpBkReader.readedXElement.getSingleText("Month");
                                tmpRetDetail.acctType = tmpBkReader.readedXElement.getSingleText("AcType");
                                tmpRetDetail.actCur = convertCurToATS(tmpBkReader.readedXElement.getSingleText("CurrencyCode"));
                                tmpRetDetail.actBal = tmpBkReader.readedXElement.getSingleText("Amount");
                                tmpRetDetail.accNbr = tmpBkReader.readedXElement.getSingleText("AcNo");

                                String dzflag = tmpBkReader.readedXElement.getSingleText("CheckFlag");//对账情况
                                if("2".equals(dzflag)){
                                    dzflag = "2";
                                }else{
                                    dzflag = "1";
                                }
                                tmpRetDetail.dzFlag = dzflag;
                                tmpRetDetail.dateFrom = beginDate;
                                tmpRetDetail.dateTo = endDate;
                                tmpRetDetail.respCode = "0";
                                tmpRetDetail.respInfo = tmpReturnMsg;
                                retAts.writeRetDetail(tmpRetDetail);
                            }
                        }else{
                            totalNum = 0;
                            retAts = new RetAtsBalCheckQry(p_req7508.reqHeader, p_req7508.actNum);
                            retAts.writeStart();//写返回ATS的统一头部信息
                            retAts.writeRespCode("-1");
                            retAts.writeRespInfo(tmpReturnMsg);
                        }
                    } else {
                        totalNum = 0;
                        retAts = new RetAtsBalCheckQry(p_req7508.reqHeader, p_req7508.actNum);
                        retAts.writeStart();//写返回ATS的统一头部信息
                        retAts.writeRespCode("-1");
                        retAts.writeRespInfo(tmpSbMsg.toString());
                    }
                } else {
                    totalNum = 0;
                    retAts = new RetAtsBalCheckQry(p_req7508.reqHeader, p_req7508.actNum);
                    retAts.writeStart();//写返回ATS的统一头部信息
                    retAts.writeRespCode("-1");
                    retAts.writeRespInfo(tmpStrRet[1]);
                }
            } while (totalNum >= startNum);
        } catch (Exception e) {
            retAts = new RetAtsBalCheckQry(p_req7508.reqHeader, p_req7508.actNum);
            retAts.writeStart();//写返回ATS的统一头部信息
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(e.getMessage());
            this.WriteBankLogLn(e);
        }
        retAts.writeEnd();
        return retAts;
    }
}
